import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import javax.swing.JPanel;

/**
 * Copyright (C) 2013  Arun Lee & Bryce Nagareda
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Draw the gnomon for horizontal sundial 
 * 	should be able to just simply height and width 
 * 	and everything would still be in scale 
 * @author Arun Lee
 */
public class DrawHGnomon extends JPanel implements Printable{
	/**Serial version uid*/
	private static final long serialVersionUID = 1L;
	private int degree;
	private String timezone = " ";
	private boolean south = false;
	private int length = 1;
	private int height = 500;
	private int width = 700;
	
	/**
	 * initialize all the necessary data  
	 * @param latitude latiude
	 * @param tz time zone
	 * @param S south
    * @param radius radius
	 */
	public DrawHGnomon(int latitude, String tz, boolean S, int radius) {
		// TODO Auto-generated constructor stub
		this.degree = latitude;
		this.timezone = tz;
		this.south = S;
		this.length = radius;
		this.width = (int) (this.length*2.8);
		this.height = (int) (this.width*0.75);
		setBackground(Color.white);	
		setPreferredSize(new Dimension(this.width, this.height));
	}
	
	/**
	 * Draw the gnomon 
	 */
	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paint(java.awt.Graphics)
	 */
	@Override
	public void paint(Graphics canvas){
		super.paintComponent(canvas);
    	canvas.setFont(new Font("Arial", Font.ITALIC, 15));
		canvas.drawString(this.timezone, (int) (this.width*0.55), (int) (this.height*0.2));
		//base of the gnomon
		int x1 = (int) (this.width*0.1), y1 = (int) (this.height*0.90), x2 = (int) (this.width*0.5), y2 = (int) (this.height*0.90);
		canvas.drawLine(x1, y1, x2, y2);
		//direction indicator, just an arrow
		canvas.drawLine((int) (x1*4.25), (int) (y1*0.97), (int) (x2*0.90), (int) (y2*0.99));
		canvas.drawLine((int) (x1*3.0), (int) (y1*0.99), (int) (x2*0.90), (int) (y2*0.99));
		//determine if we are pointing north or south
		if(this.south){
			canvas.drawString("S", (int) (x2*0.75), (int) (y2*0.98));
		}
		else {
			canvas.drawString("N", (int) (x2*0.75), (int) (y2*0.98));
		}
		//draw the angle of the gnomon
		double radians = this.degree * Math.PI / 180;
		double factor = (x2-x1)/Math.cos(radians);
    	int newY = (int)(y2 - factor * Math.sin(radians));
    	canvas.drawLine(x1, y1, x2, newY);
    	canvas.drawLine(x2, y2, x2, newY);
	}
	
	/**
	 * 
	 */
	/* (non-Javadoc)
	 * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
	 */
	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
		// number of page is zero based
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        // User (0,0) is typically outside the
        // imageable area, so we must translate
        // by the X and Y values in the PageFormat
        // to avoid clipping.
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // Now we perform our rendering
        //This prints a grey page with the applet name at the top, but no lines.
        //why no lines?
        this.paint(g2d);

        // tell the caller that this page is part
        // of the printed document
        return PAGE_EXISTS;
	}
}

